#ifndef lcthw_Stack_h
#define lcthw_Stack_h

#include <stdlib.h>

// 链表栈的结点
typedef struct StackNode {
    struct StackNode *next;
    void *value; //无类型指针，可指向任何
} StackNode;
//栈
typedef struct Stack {
    int count;
    StackNode *top;
} Stack;

#define Stack_create()   ({Stack *stack=malloc(sizeof(Stack)); stack->count = 0;stack->top = NULL; stack;})


#define Stack_count(A) (A->count)
#define Stack_peek(A) ((A)->top->value)

#define Stack_destroy(A) while(Stack_count(A)) { Stack_pop(A); } free(A);



#define STACK_FOREACH(L, V)     StackNode *V = NULL;\
    for(V = L->top; V != NULL; V  = V->next)

#define Stack_push(S,VAL) StackNode *new_node = malloc(sizeof(StackNode));\
    assert(new_node != NULL);\
    new_node->value =(VAL);\
    new_node->next = (S)->top; \
    (S)->top = new_node;\
    (S)->count++;


#define Stack_pop(S) ({StackNode *new_node = malloc(sizeof(StackNode));\
    assert(new_node != NULL);\
    new_node->value = (S)->top->value; \
    new_node->next = NULL;\
    (S)->top = (S)->top->next; \
    Stack_count((S)) = (Stack_count((S)) >0 ?((Stack_count((S)))-1):0);\
    new_node->value;})

#endif
